﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Web;
using NGCDS.Properties;

namespace NGCDS
{
    class MailUtil
    {
        private static Encoding big5enc = Encoding.GetEncoding("Big5");
        private static Settings s = Settings.Default;

        public static void send(MailAddress mailFrom, string to, string subject, string message)
        {
            // 要不要寄信阿？
            bool isEnableErrMail = s.Mail_isEnableErrMail;
            if (!isEnableErrMail)
            {
                return;
            }

            MailMessage msg = new MailMessage();

            // from
            msg.From = mailFrom;

            //to
            string receivers = to;
            string defaultErrMailReceiver = s.Mail_Default_ErrMail_Receiver;
            if (!defaultErrMailReceiver.Equals(""))
            {
                receivers += "," + defaultErrMailReceiver;
            }
            string[] receiverList = receivers.Split(",;".ToCharArray());
            foreach (string receiver in receiverList)
            {
                string receiverMail = ConvertMailAddress(receiver);
                msg.To.Add(new MailAddress(receiverMail, receiverMail, big5enc));
            }

            // subject
            msg.Subject = subject;
            msg.SubjectEncoding = big5enc;      // 設定標題編碼

            //body
            msg.IsBodyHtml = true;
            msg.Body = genMailBody(message);
            msg.BodyEncoding = big5enc;         // 設定內文編碼   

            // mail server
            SmtpClient client = new SmtpClient();
            client.Host = s.Mail_SERVER;
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            // send
            client.Send(msg);
        }

        public static void send(string to, string subject, string message)
        {
            // from
            MailAddress mailFrom = new MailAddress(s.Mail_From_Address, s.Mail_From_Display_Name, big5enc);
            send(mailFrom, to, subject, message);
        }

        private static string ConvertMailAddress(string name)
        {
            if (!name.Contains("@"))
            {
                return name + s.Mail_To_Domain_Name;
            }
            else
            {
                return name;
            }
        }

        private static string genMailBody(string s_msg)
        {

            string msg = HttpUtility.HtmlEncode(s_msg);

            // 取代取代 排版排版 
            msg = msg.Replace(" ", "&nbsp;");
            msg = msg.Replace("\r\n", "<br/>\r\n");
            
            string mailBody = @"
<html>
<body>
<div align='left'>
  <font face='Courier New' size='-1'> 
  @msg 
  </font>
</div>
<br/>
<div align='left'>
  <font face='Courier New' color='gray' size='-1'>
  ---<br/>
  發信時間：@time 
  </font>
</div>
</body>
</html>
            ";

            mailBody = mailBody.Replace("@msg", msg);
            mailBody = mailBody.Replace("@time", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

            //mailBody.Append("技術支援： ");
            //mailBody.Append("中華電信研究所 TL422 鄭廉勳<br>");
            //mailBody.Append("分機 (03)4244964 郵件 lsjeng@cht.com.tw<br>");

            return mailBody;
        }
    }
}
